#include "platform.h"
#include "thread.h"


#ifdef SP_HAVE_WINDOWS

static unsigned int __stdcall thread_routine (void *arg)
{
	thread* self = (thread*)arg;
	self->_tfn(self->_arg);
	return 0;
}

DWORD (WINAPI *PTHREAD_START_ROUTINE)(
	LPVOID lpThreadParameter
	);

void thread::start(thread_fn *tfn, void* arg)
{
	_tfn = tfn;
	_arg = arg;

	descriptor = (HANDLE)CreateThread(NULL, 0, thread_routine, this, 0, NULL);
}

void thread::stop()
{
	WaitForSingleObject(descriptor, INFINITE);
}
#else

static void* thread_routine(void *arg)
{
	thread* self = (thread*)arg;
	self->_tfn(self->_arg);
	return NULL;
}

void thread::start(thread_fn *tfn, void* arg)
{
	_tfn = tfn;
	_arg = arg;
	pthread_create(&descriptor, NULL, thread_routine, this);
}

void thread::stop()
{
	pthread_join(descriptor, NULL);
}

#endif
